﻿using CefSharp;
using CefSharp.WinForms;
using FictionsDownload.Events;
using Zzw;
using FictionsDownload.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FictionsDownload.BLL.CefSharp
{
    public abstract class CefSharpBase : ICefSharp
    {
        #region WebBrowser 构造方法
        public ChromiumWebBrowser WebBrowser { get; }

        protected CefSharpBase(string url)
        {
            //实例一个Cef设置，设置其中的本地缓存路径
            CefSettings Settings = new CefSettings();
            Settings.CachePath = System.IO.Directory.GetCurrentDirectory() + @"\Cache";
            //将Cef设置传进去，实例一个web_Auto
            Cef.Initialize(Settings);
            WebBrowser = new ChromiumWebBrowser(url);
            //WebBrowser.LifeSpanHandler = new OpenPageSelf();
            EventInit();
        }
        #endregion
        #region 公共事件和属性
        /// <summary>
        /// 有新的消息需要添加（事件声明）
        /// </summary>
        public event AddMessageEventHandler AddMessage;
        #endregion
        /// <summary>
        /// 追加事件的实现
        /// </summary>
        private void EventInit()
        {
            WebBrowser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(web_Auto_FrameLoadEnd);
            WebBrowser.AddressChanged += new EventHandler<AddressChangedEventArgs>(web_Auto_AddressChanged);
            WebBrowser.IsBrowserInitializedChanged += new EventHandler<IsBrowserInitializedChangedEventArgs>(web_Auto_IsBrowserInitializedChanged);
        }
        #region IsBrowserInitialized
        /// <summary>
        /// 浏览器是否初始化
        /// </summary>
        public bool IsBrowserInitialized { get; set; } = false;
        /// <summary>
        /// 初始化完成（事件）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void web_Auto_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
        {
            if (e.IsBrowserInitialized)
            {
                this.IsBrowserInitialized = true;
            }
        }
        #endregion
        #region CurrentWebUrl
        /// <summary>
        /// 当前浏览器网址
        /// </summary>
        public string CurrentWebUrl { get; set; }
        /// <summary>
        /// 浏览器地址发生变更
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void web_Auto_AddressChanged(object sender, AddressChangedEventArgs e)
        {
            CurrentWebUrl = e.Address;
        }
        #endregion
        #region Html
        /// <summary>
        /// 加载页面开始=false;完成=true;
        /// </summary>
        public string Html { get; set; }
        /// <summary>
        /// 页面框架加载完成后调用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void web_Auto_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            Html = await WebBrowser.GetSourceAsync();
        }

        public abstract Task<bool> LoginNameAsync();

        public abstract Task<bool> LoginAccount();

        public abstract Task<string> LoadUrlAsync(string url);
        #endregion

        #region UserName属性，触发事件
        /// <summary>
        /// 有用户名称了（已登录）
        /// </summary>
        public event EventHandler UserNameChanged;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                UserNameChanged?.Invoke(this, new TextEventArgs(value));
            }
        }
        private string _userName;
        #endregion
    }
}
